package com.huidu.bitrade.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huidu.bitrade.constant.AdvertiseType;
import com.huidu.bitrade.constant.CommonStatus;
import com.huidu.bitrade.entity.BusinessDepositStrategy;
import com.huidu.bitrade.entity.OtcCoin;
import com.huidu.bitrade.entity.transform.ScanAdvertise;
import com.huidu.bitrade.entity.transform.SpecialPage;
import com.huidu.bitrade.mapper.AdvertiseMapper;
import com.huidu.bitrade.result.page.MpPageQuery;
import com.huidu.bitrade.result.page.PageCondition;
import com.huidu.bitrade.service.AdvertiseMPService;
import com.huidu.bitrade.service.BusinessDepositStrategyService;
import com.huidu.bitrade.util.BigDecimalUtils;
import com.huidu.bitrade.util.DataMaskingUtils;
import com.huidu.bitrade.vo.AdvertiseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class AdvertiseMPServiceImpl  implements AdvertiseMPService {

    @Autowired
    private AdvertiseMapper advertiseMapper;

    @Autowired
    private BusinessDepositStrategyService businessDepositStrategyService;

    @Override
    public SpecialPage<ScanAdvertise> paginationAdvertise(MpPageQuery query, OtcCoin otcCoin, AdvertiseType advertiseType, double marketPrice, int isCertified) {
        SpecialPage<ScanAdvertise> specialPage = new SpecialPage<>();
        IPage page = PageCondition.page(query);
        List<AdvertiseVo> list = advertiseMapper.findAdvertiseList(page,marketPrice,otcCoin.getId(),advertiseType.ordinal(),isCertified);
        if (!list.isEmpty()) {
            int total = (int) page.getTotal();
            specialPage.setTotalElement(total);
            List<BusinessDepositStrategy> businessDepositStrategies =  businessDepositStrategyService.findAllByStatus(CommonStatus.NORMAL);
            Map<Long,BusinessDepositStrategy> strategyMap = businessDepositStrategies.stream().collect(Collectors.toMap(BusinessDepositStrategy::getId, Function.identity()));
            specialPage.setTotalPage((int) page.getPages());
            specialPage.setContext(
                    list.stream().map((x) ->
                            ScanAdvertise.builder()
                                    .price(BigDecimalUtils.round(Double.parseDouble(x.getFinalPrice()), 2))
                                    .transactions(x.getTransactions())
                                    .remainAmount(x.getRemainAmount())
                                    .unit(otcCoin.getUnit())
                                    .payMode(x.getPayMode())
                                    .memberName(DataMaskingUtils.mobilePhone(x.getUsername()))
                                    .avatar(x.getAvatar())
                                    .minLimit(x.getMinLimit())
                                    .maxLimit(x.getMaxLimit())
                                    .coinNameCn(otcCoin.getNameCn())
                                    .level(x.getMemberLevel())
                                    .coinId(otcCoin.getId())
                                    .coinName(otcCoin.getName())
                                    .advertiseId(x.getId())
                                    .createTime(x.getCreateTime())
                                    .advertiseType(advertiseType)
                                    .businessLevelEnum(Objects.isNull(x.getAcceptDepositId())?null:strategyMap.get(x.getAcceptDepositId()).getBusinessLevelEnum())
                                    .build()
                    ).collect(Collectors.toList()));
        } else {
            specialPage.setTotalPage(1);
            specialPage.setTotalElement(0);
        }
        specialPage.setCurrentPage((int) page.getCurrent());
        specialPage.setPageNumber((int) page.getSize());
        return specialPage;
    }
}
